<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19 0019
 * Time: 10:31
 */

namespace frontend\controllers;
// header("Access-Control-Allow-Origin:http://medicals.qingguo188.com");
// header("Access-Control-Allow-Origin:http://localhost:8080");
header("Access-Control-Allow-Methods:POST,GET");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
header("Access-Control-Allow-Credentials: true");
use common\models\User;
use common\models\UserSecondary;
use Yii;
// use yii\web\Controller;
use frontend\controllers\BaseController;
/**
 * User controller
 */
class UserSecondaryController extends BaseController
{

    public function beforeAction($action)
    {
        if ($action->id == 'addseconduser' || $action->id == 'changemessage' || $action->id == 'dodelete') {
            $this->enableCsrfValidation = false;
        }
        return parent::beforeAction($action);
    }
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [

        ];

    }

    public function actionAddseconduser()
    {
        if(Yii::$app->request->isPost){
            $data = yii::$app->request->post();
            if($data){
                //判断该手机号是否已经存在(一级和二级的都查)
                $user = new User();
                $userlist = $user::find()->where(['phone'=>$data['phone']])->one();

                $user1 = new UserSecondary();
                $seconduserlist = $user1::find()->where(['phone'=>$data['phone']])->one();

                if($userlist || $seconduserlist){
                    return $this->getjson(0,'该手机已经注册');
                }
                $model = new UserSecondary();
                $model->user_id = trim($data['userid']);
                $model->phone = trim($data['phone']);
                $model->name = trim($data['name']);
                $model->password = trim($data['password']);
                $model->site = trim($data['site']);
                $model->create_time = date('Y-m-d H:i:s',strtotime('now'));
                //存入数据库
                if($model->save()){
                    $data['id'] = $model->id;
                    return $this->getjson(1,$data);
                }else{
                    return $this->getjson(0,'参数不能为空');
                }
            }else{
                return $this->getjson(0,'参数不能为空');
            }
        }else{
            return $this->getjson(0,'后台接收参数失败,请重试');
        }

    }

    public function actionChangemessage()
    {
        if(Yii::$app->request->isPost){
            $data = yii::$app->request->post();
            //判断电话号是否已经存在了
            $user = new User();
            $userlist = $user::find()->where(['phone'=>$data['phone']])->one();
            $user1 = new UserSecondary();
            $seconduserlist = $user1::find()->where(['phone'=>$data['phone']])->one();

            if($userlist || $seconduserlist){
                return $this->getjson(0,'该手机已经注册');
            }
            if($data){
                if($data['userid']){
                    //二级用户
                    $model = UserSecondary::findOne($data['id']);
                    $model->name = $data['name'];
                    $model->password = $data['password'];
                    $model->phone = $data['phone'];
                    $model->site = $data['site'];
                    if($model->save()){
                        return $this->getjson(1,'更新成功');
                    }else{
                        return $this->getjson(0,'更新失败,请重试');
                    }
                }else{
                    //一级用户
                    $user = User::findOne($data['id']);
                    $user->name = $data['name'];
                    $user->password = $data['password'];
                    $user->phone = $data['phone'];
                    $user->site = $data['site'];
                    if($user->save()){
                        return $this->getjson(1,'更新成功');
                    }else{
                        return $this->getjson(0,'更新失败,请重试');
                    }
                }
            }else{
                return $this->getjson(0,'参数不能为空');
            }
        }else{
            return $this->getjson(0,'后台接收参数失败,请重试');
        }
    }

    public  function  actionDodelete()
    {
        if(Yii::$app->request->isPost){
            $data = yii::$app->request->post();
            if($data){
                //删除用户,删除用户添加的药,删除记录
                $model = UserSecondary::findOne($data['id']);
                if($model){
                    if($model->delete()){
                        return $this->getjson(1,'删除成功');
                    }else{
                        return $this->getjson(0,'删除失败,请重试');
                    }
                }else{
                    return $this->getjson(0,'二级用户中不存在该用户id');
                }
            }else{
                return $this->getjson(0,'参数不能为空');
            }
        }else{
            return $this->getjson(0,'后台接收参数失败,请重试');
        }
    }
}




















